package com.tianyu.inspection.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.entity.vo.Ro;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.tianyu.inspection.service.websocket.TelemetryQueue.telemetryQueueMap;

/**********************************
 * @Author YSW
 * @Description 日志打印接口
 * @Date 2021/7/29 - 10:47
 **********************************/

@RestController
@RequestMapping("/api/2.0/sys/admin")
@Slf4j
public class SysController {

    @GetMapping(value = "/telemetryQueue")
    @Permission(value = 1,tags = Constants.API_SUPER)
    public Ro<ModelMap> logTelemetryQueue(){
        Ro<ModelMap> ro = new Ro<>();
        try {
            final Map<String, JSONArray> map = new HashMap<>(16);
            telemetryQueueMap.forEach((key, telemetryQueue) -> {
                String[] values = key.split("-&-");
                final JSONArray projectQueueList = map.getOrDefault(values[0], new JSONArray());
                if ("yuKong".equals(values[1])){
                    final JSONObject jsonObject = new JSONObject();
                    jsonObject.put("projectId",values[0]);
                    jsonObject.put("source","御空");
                    jsonObject.put("taskId",values[2]);
                    jsonObject.put("dataCount",telemetryQueue.size());
                    projectQueueList.add(jsonObject);
                }else {
                    final JSONObject jsonObject = new JSONObject();
                    jsonObject.put("projectId",values[0]);
                    jsonObject.put("source","云盒");
                    jsonObject.put("boxSn",values[1]);
                    jsonObject.put("taskId",values[2]);
                    jsonObject.put("dataCount",telemetryQueue.size());
                    /*if (telemetryQueue.size() > 0){
                        final Telemetry.LiveData liveData = telemetryQueue.peek();
                        jsonObject.put("taskId",liveData.getTaskId());
                        jsonObject.put("uavId",liveData.getUavId());
                        jsonObject.put("uavName",liveData.getUavName());
                        jsonObject.put("projectId",liveData.getProjectId());
                        jsonObject.put("latitude",liveData.getLatitude());
                        jsonObject.put("longitude",liveData.getLongitude());
                        jsonObject.put("pitch",liveData.getPitch());
                        jsonObject.put("roll",liveData.getRoll());
                        jsonObject.put("yaw",liveData.getYaw());
                        jsonObject.put("ultrasonicHeight",liveData.getUltrasonicHeight());
                        jsonObject.put("altitudeHeight",liveData.getAltitudeHeight());
                        jsonObject.put("airspeed",liveData.getAirspeed());
                        jsonObject.put("velocity",liveData.getVelocity());
                        jsonObject.put("timestamps",liveData.getTimestamps());
                        jsonObject.put("source",liveData.getSource());
                        jsonObject.put("ptPitch",liveData.getPtPitch());
                        jsonObject.put("ptRoll",liveData.getPtRoll());
                        jsonObject.put("ptYaw",liveData.getPtYaw());
                    }*/
                    projectQueueList.add(jsonObject);
                    map.put(values[0],projectQueueList);
                }
            });
            final ModelMap modelMap = new ModelMap();
            modelMap.put("count",telemetryQueueMap.size());
            modelMap.put("telemetryQueue",map);
            return ro.setContent(modelMap);
        }catch (Exception e){
            return ro.setResultEnum(RoState.FAIL);
        }
    }
}
